// 导入全局参数
import G from '@/config/global.js';
// 导入请求接口
import {
	requestApi,
	uphttp
} from '@/utils/request.js';
// 导入地址配置表
import apis from '@/api/index.js';

export default {
	data() {
		return {
			// 全局参数
			G: G,
		}
	},
	methods: {
		/***
		 * @description: 跳转地址
		 * @param {url} 跳转地址
		 * @param {type} 跳转方式(navigateTo,switchTab)
		 * @return {*}
		 * @author: white_queen
		 */
		jump(url, type) {
			try {
				switch (type) {
					case 'navigateBack':
						uni.navigateBack({
							url: url
						})
						break;
					case 'navigateTo':
						uni.navigateTo({
							url: url
						})
						break;
					case 'switchTab':
						uni.switchTab({
							url: url
						})
						break;
					default:
						break;
				}
			} catch (e) {
				uni.showToast({
					icon: "none",
					title: e.message ? e.message : this.$t('sysTip')
				})
			}
		},
		// 返回
		goBack() {
			let pages = getCurrentPages();
			if (!uni.getStorageSync('X-CSRF-TOKEN')) {
				uni.redirectTo({
					url: "/pages/index/index"
				})
				return false;
			}
			if (pages.length > 1) {
				uni.navigateBack();
			} else {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			}
		},
		/***
		 * @description: 复制文本
		 * @param {*}
		 * @return {*}
		 * @author: white_queen
		 */
		copy(text) {
			uni.setClipboardData({
				data: text,
				success: () => {
					uni.showToast({
						icon: 'none',
						title: this.$t('copy')
					})
				},
				fail: () => {
					uni.showToast({
						icon: 'none',
						title: this.$t('copy2')
					})
				}
			});
		},
		// 发起邮箱验证码
		sendSms(email) {
			return new Promise((resolve, reject) => {
				requestApi(apis.login.sendSms, {
					email: email
				}, 'POST').then(res => {
					uni.hideLoading();
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				}).catch(res => {
					uni.showToast({
						icon: 'none',
						title: res.msg
					});
				});
			})
		}
	}
}